package com.unnamed.b.atv.sample.ability;

import com.unnamed.b.atv.model.TreeNode;
import com.unnamed.b.atv.sample.ResourceTable;
import com.unnamed.b.atv.sample.holder.ArrowExpandSelectableHeaderHolder;
import com.unnamed.b.atv.sample.holder.IconTreeItemHolder;
import com.unnamed.b.atv.view.OhosTreeView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.window.dialog.ToastDialog;

/**
 * TwoDScrollingArrowExpandAbility
 *
 * @author Administrator
 * @Date 2021/5/26
 */
public class TwoDScrollingArrowExpandAbility extends Ability implements TreeNode.TreeNodeClickListener {
    private static final String NAME = "Very long name for folder";

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void onActive() {
        super.onActive();
        super.setUIContent(ResourceTable.Layout_ability_selectable_nodes);
        initView();
    }

    private void initView() {
        ComponentContainer dependentLayout = (ComponentContainer) findComponentById(ResourceTable.Id_container_select);
        DirectionalLayout status = (DirectionalLayout) findComponentById(ResourceTable.Id_status);
        status.setVisibility(Component.HIDE);
        TreeNode root = TreeNode.root();
        TreeNode s1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_folder_close,
                "Folder with very long name ")).setViewHolder(new ArrowExpandSelectableHeaderHolder(getContext()));
        TreeNode s2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(ResourceTable.Media_folder_close,
                "Another folder with very long name"))
                .setViewHolder(new ArrowExpandSelectableHeaderHolder(getContext()));
        fillFolder(s1);
        fillFolder(s2);
        root.addChildren(s1, s2);
        OhosTreeView tView = new OhosTreeView(getContext(), root);
        tView.setDefaultAnimation(true);
        tView.setUse2dScroll(true);
        tView.setDefaultNodeClickListener(TwoDScrollingArrowExpandAbility.this);
        tView.setDefaultViewHolder(ArrowExpandSelectableHeaderHolder.class);
        dependentLayout.addComponent(tView.getView());
        tView.setUseAutoToggle(false);
        tView.expandAll();
    }

    private void fillFolder(TreeNode folder) {
        TreeNode currentNode = folder;
        for (int i = 0; i < 4; i++) {
            TreeNode file = new TreeNode(new IconTreeItemHolder.IconTreeItem
                    (ResourceTable.Media_folder_close, NAME + " " + i));
            currentNode.addChild(file);
            currentNode = file;
        }
    }

    @Override
    public void onClick(TreeNode node, Object value) {
        new ToastDialog(getContext()).setText(((IconTreeItemHolder.IconTreeItem)value).getText()).show();
    }

}
